package com.yh.modules.data.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yh.common.utils.Assert;
import com.yh.common.utils.PageUtils;
import com.yh.common.utils.Query;
import com.yh.common.utils.R;
import com.yh.modules.data.dao.MachineWorkRecordDao;
import com.yh.modules.data.entity.JqGridEntity;
import com.yh.modules.data.entity.MachineEntity;
import com.yh.modules.data.entity.MachineWorkRecordEntity;
import com.yh.modules.data.service.MachineService;
import com.yh.modules.data.service.MachineWorkRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.function.ToIntFunction;
import java.util.stream.Collectors;

/**
 * @Author: skh
 * @Date: 2020/3/31 16:08
 * @Description:
 */
@Service
@Slf4j
public class MachineWorkRecordServiceImpl extends ServiceImpl<MachineWorkRecordDao, MachineWorkRecordEntity> implements MachineWorkRecordService {

    @Autowired
    private MachineService machineService;

    @Override
    public void saveWorkRecord(MachineWorkRecordEntity workRecordEntity) {

        Date date = new Date();
        String dateStr = DateUtil.formatDate(date);
        workRecordEntity.setDate(dateStr);
        workRecordEntity.setCreateTime(date);

        this.insert(workRecordEntity);
    }

    @Override
    public Page<MachineWorkRecordEntity> getWorkRecordList(Map<String, Object> params) {
        Integer machineId = MapUtil.getInt(params, "machineId");
        Assert.isNull(machineId, "机器id不能为空");
        String time = MapUtil.getStr(params, "time");

        if (StrUtil.isBlank(time)) {
            time = DateUtil.formatDate(new Date());
        }

        EntityWrapper<MachineWorkRecordEntity> wrapper = new EntityWrapper<>();
        wrapper.eq("machine_id", machineId);
        wrapper.eq("date", time);
        wrapper.orderBy("create_time", true);
        Page<MachineWorkRecordEntity> page = this.selectPage(new Query<MachineWorkRecordEntity>(params).getPage(), wrapper);

        return page;

    }

    @Override
    public JqGridEntity getWorkRecordColNamesAndModel(Integer machineId) {
        List<String> colNameList = CollUtil.newArrayList("记录id", "机器工作开始时间", "机器工作结束时间", "总数量");
        log.debug("表头列表:{}", colNameList);

        List<String> colModelList = CollUtil.newArrayList("id", "startTime", "endTime", "totalCount");
        log.debug("模型列表:{}", colModelList);

        //根据colModelList拼装colModel
        //{ label: '项目id', name: 'id', index: 'id', width: 50,sortable: false, key: true },
        List<Map<String, Object>> colModelEntity = assembleJqidColModelList(colNameList, colModelList);
        log.debug("colModel实体:{}", colModelEntity);

        MachineEntity machineEntity = machineService.selectById(machineId);


        JqGridEntity jqGridEntity = new JqGridEntity();
        jqGridEntity.setColNameList(colNameList);
        jqGridEntity.setColModelList(colModelList);
        jqGridEntity.setColModelEntity(colModelEntity);
        jqGridEntity.setMachineEntity(machineEntity);
        return jqGridEntity;
    }

    @Override
    public List<MachineWorkRecordEntity> getWorkRecordListByEcharts(Map<String, Object> params) {
        Integer machineId = MapUtil.getInt(params, "machineId");
        Assert.isNull(machineId, "机器id不能为空");
        String time = MapUtil.getStr(params, "time");

        if (StrUtil.isBlank(time)) {
            time = DateUtil.formatDate(new Date());
        }

        EntityWrapper<MachineWorkRecordEntity> wrapper = new EntityWrapper<>();
        wrapper.eq("machine_id", machineId);
        wrapper.eq("date", time);
        wrapper.orderBy("create_time", true);
        List<MachineWorkRecordEntity> machineWorkRecordEntityList = this.selectList(wrapper);
        for (MachineWorkRecordEntity machineWorkRecordEntity : machineWorkRecordEntityList) {
            Date startTime = machineWorkRecordEntity.getStartTime();
            String sTime = DateUtil.formatTime(startTime);

            Date endTime = machineWorkRecordEntity.getEndTime();
            String eTime = DateUtil.formatTime(endTime);

            machineWorkRecordEntity.setStartTimeAndEndTime(sTime + "-" + eTime);
        }

        return machineWorkRecordEntityList;

    }

    private List<Map<String, Object>> assembleJqidColModelList(List<String> colNameList, List<String> colModelList) {
        List<Map<String, Object>> colModelEntity = new ArrayList<>();
        for (int i = 0; i < colNameList.size(); i++) {
            Map<String, Object> colModel = new HashMap<>();
            colModel.put("label", colNameList.get(i));
            colModel.put("name", colModelList.get(i));
            colModel.put("sortable", false);
            colModelEntity.add(colModel);
        }
        return colModelEntity;
    }
}
